package com.diaohw.platform.framework.db.obj;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * @author 刁海文
 * @Remark 公用基础model 包含创建和修改
 * @ignore
 * @since 2023/6/27 21:29
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Accessors(chain = true)
public class BaseModel<T> extends Model<BaseLogModel<T>> {
    /**
     * 主键ID
     */
    @Schema(description = "ID")
    @TableId(value = "ID", type = IdType.ASSIGN_ID)
    private String id;

    /**
     * 创建时间
     */
    @Schema(description = "创建时间")
    @TableField(value = "created_time", fill = FieldFill.INSERT)
    private LocalDateTime createdTime;

    /**
     * 修改时间
     */
    @Schema(description = "修改时间")
    @TableField(value = "updated_time", fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updatedTime;

    /**
     * 创建人
     */
    @Schema(description = "创建人")
    @TableField(value = "created_by", fill = FieldFill.INSERT)
    private String createdBy;

    /**
     * 更新人
     */
    @Schema(description = "更新人")
    @TableField(value = "updated_by", fill = FieldFill.INSERT_UPDATE)
    private String updatedBy;

    @Override
    public Serializable pkVal() {
        return this.id;
    }

    public void clear() {
        this.createdTime = null;
        this.updatedTime = null;
        this.createdBy = null;
        this.updatedBy = null;
        this.id = null;
    }


    public String initPkVal() {
        String pkId = DefaultIdentifierGenerator.getInstance().nextId(this).toString();
        this.id = pkId;
        return pkId;
    }

    public static String primaryKey(Object data) {
        return DefaultIdentifierGenerator.getInstance().nextId(data).toString();
    }
}
